#include "historyexperiment.h"

HistoryExperiment::HistoryExperiment()
{

}



QVariantList HistoryExperiment::getHistoryList(QVariantMap filter_line, QVariantMap filter_detail){



    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select Id, ExperimentName, EquName, AmplName, FinishTime, EquId, AmplGuid, Code, TestKit, System, ConsumableType, RunTime "
                "From   ExperimentInfo "
                "Where  Status='完成'"
                "Order By FinishTime DESC";
    query.exec(sql);
    QVariantList res;
    int idx = 1;
    while(query.next()){
        QVariantMap line;
        line["Id"] = query.value("Id").toString();
        line["Idx"] = idx;
        line["ExpName"] = query.value("ExperimentName").toString();
        line["EquName"] = query.value("EquName").toString();
        line["ProgramName"] = query.value("AmplName").toString();
        line["Code"] = query.value("Code").toString();
        line["TestKit"] = query.value("TestKit").toString();
        line["System"] = query.value("System").toString();
        line["Consume"] = query.value("ConsumableType").toString();
        line["CreateTime"] = query.value("RunTime").toString();

        QDateTime time1 = QDateTime::fromString(query.value("RunTime").toString(), "yyyy-MM-dd hh:mm:ss");

        QDateTime time2 = QDateTime::fromString(query.value("FinishTime").toString(), "yyyy-MM-dd hh:mm:ss");

        int seconds = time1.secsTo(time2);

        line["RunTime"] = QString::number(std::floor(seconds / 3600)) + "时" + QString::number(int(std::floor(seconds / 60)) % 60) + "分" + QString::number(seconds % 60) + "秒";

        // 条件筛选
        if(query.value("ExperimentName").toString().contains(filter_line["ExpName"].toString()) &&
           query.value("EquName")       .toString().contains(filter_line["EquName"].toString()) &&
           query.value("AmplName")   .toString().contains(filter_line["ProgramName"].toString()) &&
           query.value("Code")          .toString().contains(filter_line["Code"].toString()) &&
           query.value("ConsumableType").toString().contains(filter_line["ConsumbaleType"].toString()) &&
           query.value("System").toString().contains(filter_line["System"].toString()) &&
           query.value("TestKit")       .toString().contains(filter_line["TestKit"].toString())){

            if(filter_line["FinishTime1"]!=""){
                if(line["CreateTime"].toString().split(" ")[0] < filter_line["FinishTime1"].toString()){
                    continue;
                }
            }if(filter_line["FinishTime2"]!=""){
                if(line["CreateTime"].toString().split(" ")[0] > filter_line["FinishTime2"].toString()){
                    continue;
                }
            }

            if(getDetailList(query.value("Id").toString(), filter_detail).length() != 0 || filter_detail["SampleName"]==""){
                res.push_back(line);
                idx ++;
            }

        }
    }
    return res;
}

int HistoryExperiment::datao(QVariantMap filter, QVariantMap filter_detail){



    QString outpath = QFileDialog::getSaveFileName(nullptr, "保存", "/path/to/default/实验数据"+QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss"),  "Xlsx files(*.xlsx)");
    if(outpath.isEmpty()|| outpath.isNull()){
        return 2;
    }

    QVariantList list = getHistoryList(filter, filter_detail);

    QXlsx::Document xlsx;

    xlsx.addSheet("实验数据");
    xlsx.write(1, 1,  "序号");
    xlsx.write(1, 2,  "实验名称");
    xlsx.write(1, 3,  "设备名称");
    xlsx.write(1, 4,  "扩增程序");
    xlsx.write(1, 5,  "编码");
    xlsx.write(1, 6,  "试剂盒");
    xlsx.write(1, 7,  "体系(μl)");
    xlsx.write(1, 8,  "耗材类型");
    xlsx.write(1, 9,  "运行时间");
    xlsx.write(1, 10, "创建时间");

    int row = 2;
    for(int i = 0; i < list.size(); i ++){
        QVariantMap line = list[i].value<QVariantMap>();
        xlsx.write(row, 1,  line["Idx"].toString());
        xlsx.write(row, 2,  line["ExpName"].toString());
        xlsx.write(row, 3,  line["EquName"].toString());
        xlsx.write(row, 4,  line["ProgramName"].toString());
        xlsx.write(row, 5,  line["Code"].toString());
        xlsx.write(row, 6,  line["TestKit"].toString());
        xlsx.write(row, 7,  line["System"].toString());
        xlsx.write(row, 8,  line["Consume"].toString());
        xlsx.write(row, 9,  line["RunTime"].toString());
        xlsx.write(row, 10, line["CreateTime"].toString());
        row ++;
    }
    if(xlsx.saveAs(outpath)){
        return 1;
    }else{
        return 0;
    }
}


QVariantList HistoryExperiment::getDetailList(QString expId, QVariantMap filter){

    QVariantList res;
    qDebug() << filter;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select *"
                        "From  HistoryExperimentDetails "
                        "Where ExperimentId='%1'").arg(expId);
    query.exec(sql);
    while(query.next()){
        QVariantList line;
        bool flag = true;
        for(int i = 2; i < query.record().count(); i ++){
            QString value = query.value(i).toString();
            if(i == 3){
                if(!value.contains(filter["SampleName"].toString())) {flag = false; break;}
            }
            line.push_back(query.value(i));
        }

        if(flag){
            res.push_back(line);
        }

    }

    return res;
}
